import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 */

/**
 * @author Lucky
 *
 */
public class FileListPopulate {

	/**
	 * @param args
	 */
	private String file="";
	private String svnid="";
	
	private int id=1;
	
	private Connection conn=null;
	
	public void getFiles()
	{
		String query1= "SELECT distinct file_changed FROM svn_info";
		try {
			Statement stmt=conn.createStatement();
			stmt.execute(query1);
			ResultSet res=stmt.getResultSet();
			while(res.next())
			{
				file=res.getString("file_changed");
				populateFileList();
				getsvn();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void populateFileList()
	{
		String file1=file.trim();
		int begin=file1.indexOf('>');
		int end=file1.indexOf('<');
		if(end == -1)
			end= file1.length();
		file1=file1.substring(begin+1, end).trim();
		String query="Insert into file_list values ('File"+id+"','"+file1+"')";
		Statement stmt=null;
		try {
			stmt = conn.createStatement();
			stmt.executeUpdate(query);
			if(stmt != null)
				stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public void getsvn()
	{
		String query="SELECT svnid FROM svn_info where file_changed='"+file+"'";
		try {
			Statement stmt=conn.createStatement();
			stmt.execute(query);
			ResultSet res=stmt.getResultSet();
			while(res.next())
			{
				svnid=res.getString("svnid");
				populateFidSvnid();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		id++;
	}
	
	public void populateFidSvnid()
	{
		String query="Insert into fid_svnid values ('File"+id+"','"+svnid+"')";
		try {
			Statement stmt=conn.createStatement();
			stmt.executeUpdate(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		FileListPopulate flp=new FileListPopulate();
		db_conn condb=new db_conn();
		flp.conn=condb.connect("2jboss","root","123");
		flp.getFiles();
		
	}

}
